;(function($){
    function SearchInit(){
       var $morphSearch = $('#morphsearch'),
        $input = $morphSearch.find( 'input.morphsearch-input' ),
        $ctrlClose = $morphSearch.find( 'span.morphsearch-close' ),
        isOpen = isAnimating = false,
        toggleSearch = function(evt) {
                            // return if open and the input gets focused
                            if( evt.type.toLowerCase() === 'focus' && isOpen ) return false;

                            var offsets = $morphSearch.offset();
                            if( isOpen ) {
                                $morphSearch.removeClass('open');                       
                                $( window ).unbind( "scroll" );
                                // trick to hide input text once the search overlay closes 
                                // todo: hardcoded times, should be done after transition ends
                                if( $input.val() !== '' ) {
                                    setTimeout(function() {
                                        $morphSearch.addClass('hideInput');    
                                
                                        setTimeout(function() {
                                             $morphSearch.removeClass('hideInput');    
                                
                                            $input.val('');
                                        }, 300 );
                                    }, 500);
                                }
                                
                                $input.blur();
                            }
                            else {
                                $morphSearch.addClass('open');    
                                $(window).scroll(function(){ $(window).scrollTop(0); });
                            }
                            isOpen = !isOpen;
                        };
                        // events
                    $input.focus(toggleSearch);
                    $ctrlClose.click(toggleSearch);
                    // esc key closes search overlay
                    // keyboard navigation events
                    $(document).keydown(function(ev) {
                        var keyCode = ev.keyCode || ev.which;
                        if( keyCode === 27 && isOpen ) {
                            toggleSearch(ev);
                        }
                    });
                }

        function ModalEffects(){
            var $overlay = $('.md-overlay');
            $('.md-trigger').each(function(index, val) {
                 var $modal = $( '#' + val.getAttribute( 'data-modal' ) ),
                 $close = $modal.find( '.md-close' );

                 function removeModal() {
                    $modal.removeClass('md-show' );
                }
                $(this).click(function(event) {
                    $modal.addClass('md-show' );
                    $overlay.click(removeModal);
                });
                $close.click(function(event) {
                    event.stopPropagation();
                    removeModal();
                });
            });
        }
             
        function PageLoad(){
                var $pageWrap = $( '#pagewrap' ),
                    $mainPage = $pageWrap.children('#page-main'),
                    $registerPage = $pageWrap.children('#page-register'),
                    $contactPage = $pageWrap.children('#page-contact'),
                    $teamPage = $pageWrap.children('#page-team'),
                    $triggerLoading = $pageWrap.find( 'a.pageload-link' ),
                    loaderRegister = new SVGLoader( document.getElementById( 'loader-register' ), { speedIn : 200, easingIn : mina.linear } );
                    loaderTeam = new SVGLoader( document.getElementById( 'loader-team' ), { speedIn : 500, easingIn : mina.easeinout } ),
                    loaderContact = new SVGLoader( document.getElementById( 'loader-contact' ), { speedIn : 500, easingIn : mina.easeinout } ),
                    isLoad = false;
                    $.each($triggerLoading, function(index, val) {
                         $(this).click(function(event) {
                             event.preventDefault();
                             if(isLoad) return false;
                             var href = $(this).attr('href');
                             if(href === '#page-main-register'){
                                loaderRegister.show();
                                 setTimeout( function() {
                                    loaderRegister.hide();
                                    $mainPage.addClass('show');
                                    $registerPage.removeClass('show');
                                    
                                     isLoad = false;
                                }, 2000 );
                             }else if(href === '#page-register'){
                                 loaderRegister.show();
                                 setTimeout( function() {
                                    loaderRegister.hide();
                                    $registerPage.addClass('show');
                                    $mainPage.removeClass('show');
                                     isLoad = false;
                                }, 2000 );
                             }else if(href === '#page-main-team'){
                                 loaderTeam.show();
                                 setTimeout( function() {
                                    loaderTeam.hide();
                                    $mainPage.addClass('show');
                                    $teamPage.removeClass('show');
                                     isLoad = false;
                                }, 2000 );
                             }else if(href === '#page-team'){
                                 loaderTeam.show();
                                 setTimeout( function() {
                                    loaderTeam.hide();
                                    $teamPage.addClass('show');
                                    $mainPage.removeClass('show');
                                     isLoad = false;
                                }, 2000 );
                             }else if(href === '#page-contact'){
                                 loaderContact.show();
                                 setTimeout( function() {
                                    loaderContact.hide();
                                    $contactPage.addClass('show');
                                    $mainPage.removeClass('show');
                                     isLoad = false;
                                }, 2000 );
                             }else if(href === '#page-main-contact'){
                                 loaderContact.show();
                                 setTimeout( function() {
                                    loaderContact.hide();
                                    $mainPage.addClass('show');
                                    $contactPage.removeClass('show');
                                     isLoad = false;
                                }, 2000 );
                             }
                         });
                    });
                
        }

        function RegisterForm(){
            var formWrap = document.getElementById( 'fs-form-wrap' );
            new FForm( formWrap, {
                    onReview : function() {
                        classie.add( document.body, 'overview' ); // for demo purposes only
                        $(".review-back-link").show("normal");
                    }
                } );
        }

        var support = { animations : Modernizr.cssanimations },
        container = document.getElementById( 'ip-container' ),
        header = container.querySelector( 'header.ip-header' ),
        loader = new PathLoader( document.getElementById( 'ip-loader-circle' ) ),
        animEndEventNames = { 'WebkitAnimation' : 'webkitAnimationEnd', 'OAnimation' : 'oAnimationEnd', 'msAnimation' : 'MSAnimationEnd', 'animation' : 'animationend' },
        // animation end event name
        animEndEventName = animEndEventNames[ Modernizr.prefixed( 'animation' ) ];

        function PreLoad(){
                var onEndInitialAnimation = function() {
                            if( support.animations ) {
                                this.removeEventListener( animEndEventName, onEndInitialAnimation );
                            }

                            startLoading();
                        };

                        // disable scrolling
                        window.addEventListener( 'scroll', noscroll );

                        // initial animation
                        classie.add( container, 'loading' );

                        if( support.animations ) {
                            container.addEventListener( animEndEventName, onEndInitialAnimation );
                        }
                        else {
                            onEndInitialAnimation();
                        }
        }

        function startLoading() {
        // simulate loading something..
                var simulationFn = function(instance) {
                    var progress = 0,

                        interval = setInterval( function() {
                            if(progress!==1) progress = Math.min( progress + Math.random() * 0.1, 0.9 );

                            instance.setProgress( progress );

                            // reached the end
                            if( progress === 1 ) {
                                classie.remove( container, 'loading' );
                                classie.add( container, 'loaded' );
                                clearInterval( interval );

                                var onEndHeaderAnimation = function(ev) {
                                    if( support.animations ) {
                                        if( ev.target !== header ) return;
                                        this.removeEventListener( animEndEventName, onEndHeaderAnimation );
                                    }

                                    classie.add( document.body, 'layout-switch' );
                                    window.removeEventListener( 'scroll', noscroll );
                                };

                                if( support.animations ) {
                                    header.addEventListener( animEndEventName, onEndHeaderAnimation );
                                }
                                else {
                                    onEndHeaderAnimation();
                                }
                            }
                        }, 80 );
                        $(window).load(function(){
                            progress=1;
                        });
                };

                loader.setProgressFn( simulationFn );
             }

         $(window).load(function(){
            $(".slideshow figure").mCustomScrollbar({
                    scrollInertia:100,
                    autoDraggerLength:false,
                    theme:"dark-thin",
                    axis:"y",
                });
        });










            ModalEffects();
            SearchInit();
            PageLoad();
            RegisterForm();
            var grid = new CBPGridGallery( document.getElementById( 'grid-gallery' ) );
            var table = new StickHeaderTable(document.getElementById( 'fix-table-content' ));
            PreLoad();
            new CBPFWTabs( document.getElementById( 'tabs' ) ,{},function(idx){
                if(idx === 0){
                    grid._initMasonry();
                }else if(idx === 3){
                    table.setWidths();
                }
            });
            function noscroll() {
                window.scrollTo( 0, 0 );
            }

})(jQuery)